class Set<T> {
  items: Record<keyof any, T>;
  constructor() {
    this.items = {};
  }

  has(value: keyof any): boolean {
    return this.items.hasOwnProperty(value);
  }

  add(value: any): boolean {
    if (this.has(value)) return false;
    this.items[value] = value;
    return true;
  }

  remove(value: any): boolean {
    if (!this.has(value)) return false;
    return delete this.items[value];
  }

  clear() {
    this.items = {};
  }

  size(): number {
    return Object.keys(this.items).length;
  }

  values() {
    return Object.values(this.items);
  }

  // union() 求两个集合的并集
  union(otherSet: Set<T>) {
    let unionSet = new Set<T>();

    for (const value of this.values()) {
      unionSet.add(value);
    }

    for (const value of otherSet.values()) {
      unionSet.add(value);
    }

    return unionSet;
  }

  // intersection() 求两个集合的交集
  intersection(otherSet: Set<T>) {
    let intersectionSet = new Set();
    for (const value of this.values()) {
      if (otherSet.has(value as keyof any)) {
        intersectionSet.add(value);
      }
    }

    return intersectionSet;
  }

  // difference() 差集
  difference(otherSet: Set<T>) {
    let differenceSet = new Set();
    for (const value of this.values()) {
      if (!otherSet.has(value as keyof any)) {
        differenceSet.add(value);
      }
    }

    return differenceSet;
  }

  // subset() 子集
  subset(otherSet: Set<T>) {
    for (const value of this.values()) {
      if (!otherSet.has(value as keyof any)) {
        return false;
      }
    }

    return true;
  }
}

export {};
